﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows;
using MediaAssistant.Controls.WaitScreen;
using MediaAssistant.Data;
using MefBasic;
using MefBasic.Commans;
using MefBasic.Threading;
using MediaAssistant.DAL;
using MediaAssistant.DAL.Constants;

namespace MediaAssistant.Controls.MovieDetail
{
    [Export]
    public class MovieDetailPresenter:APresenter<MovieDetailView>
    {
        [ImportingConstructor]
        public MovieDetailPresenter(MovieDetailView view) : base(view)
        {
            SelectYearCommand = new DelegateCommand(ExecuteSelectYear);
            ShowRecommendedMoviesCommand = new DelegateCommand(ExecuteShowRecommendedMovies);
            SelectRatedLibraryItemCommand = new DelegateCommand(ExecuteSelectRatedLibraryItem);
        }

        public DelegateCommand SelectRatedLibraryItemCommand { get; set; }

        private void ExecuteSelectRatedLibraryItem(object obj)
        {
            var item = DatabaseManager.Instance.GetRatedLibraryItem(DataSource.SelectedMovie.Rated);
            if(item!=null)
            {
                DataSource.SelectedLibraryItem = item;
            }
        }

        public DelegateCommand ShowRecommendedMoviesCommand { get; set; }
        
        [Import]
        private WaitScreenPresenter WaitScreen { get; set; }
        private void ExecuteShowRecommendedMovies(object obj)
        {
            WaitScreen.Show();
            var recommendedMovies = GetRecommendedMovies();
            DataSource.SetMovieResults(recommendedMovies);
            WaitScreen.Hide();
        }

        private IEnumerable<Movie> GetRecommendedMovies()
        {
            var job = new Job(GetRecommendedMovies);
            job.Store.Add(DataSource.SelectedMovie);
            job.Store.Add(DataSource);
            job.StartBlocked();
            var recommendedMovies = job.Store.GetObject<Movie[]>();
            return recommendedMovies;
        }

        private static void GetRecommendedMovies(Job obj)
        {
            var dataSource = obj.Store.GetObject<LibraryDataSource>();
            var selectedMovie = obj.Store.GetObject<Movie>();
            var recommendedMovies = DatabaseManager.Instance.GetRecommendedMovies(selectedMovie.Title).ToArray();
            obj.Store.Add(recommendedMovies);

        }

        public DelegateCommand SelectYearCommand { get; set; }

        private void ExecuteSelectYear(object obj)
        {
            if(DataSource.SelectedMovie.Year.HasValue)
                DataSource.SelectYear(DataSource.SelectedMovie.Year.Value);
        }


        public DelegateCommand SaveMovieStateCommand { get; set; }

        

        [Import]
        public LibraryDataSource DataSource { get; set; }

        public bool Watched
        {
            get { return (bool)GetValue(WatchedProperty); }
            set { SetValue(WatchedProperty, value); }
        }

        public static readonly DependencyProperty WatchedProperty =
            DependencyProperty.Register("Watched", typeof(bool), typeof(MovieDetailPresenter), new UIPropertyMetadata(false));



        public bool IsStar
        {
            get { return (bool)GetValue(IsStarProperty); }
            set { SetValue(IsStarProperty, value); }
        }

        public static readonly DependencyProperty IsStarProperty =
            DependencyProperty.Register("IsStar", typeof(bool), typeof(MovieDetailPresenter), new UIPropertyMetadata(false));


        
    }
}
